<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <zxxjjforever@163.com>
// +----------------------------------------------------------------------
namespace app\home\controller;

use app\home\model\MemberModel;
use app\home\model\RechargeModel;
use cmf\controller\UserBaseController;
use think\Db;


class MemberController extends UserBaseController
{
    public function index(){

    return $this->fetch();
    }

    function memadd(){
        $uinfo=cmf_get_current_user();
        $res  = $this->request->param();
        if($this->request->isPost()){
            if(empty($_POST['mobile'])){
                $this->error('手机号必填！');
            }
            if(!preg_match("/^1[34578]{1}\d{9}$/",$_POST['mobile'])){
                $this->error('手机号格式不正确！');
            }
            //判断此手机号是不是已经注册
            $check=DB::name('Member')->where(array('mobile'=>$_POST['mobile']))->find();
            if($check){
                $this->error('该手机号已是会员');exit;
            }
            $_POST['birthday']=strtotime($_POST['birthday']);
            $_POST['ctime']=time();
            $_POST['store_id']=$uinfo['store_id'];
            $re=Db::name('Member')->insert($_POST);
            
            $uinfo=Db::name('Member')->where(array('mobile'=>$_POST['mobile']))->find();
            //账户余额开户
            $da_finance=array(
                'userid'=>$uinfo['id'],
                'storeid'=>$uinfo['store_id'],                
            );
            $re=Db::name('Finance')->insert($da_finance);
            //积分开户
            $da_integral=array(
                'userid'=>$uinfo['id'],
                'storeid'=>$uinfo['store_id'],
            );
            $re=Db::name('Integral')->insert($da_integral);
            if($re){
                echo "<script>alert('添加成功')</script>";               
            }
        }
        return $this->fetch();
    }
    public function center(){
      return $this->fetch();
    }   
    //输入会员手机号
    function recharge(){
        $uinfo=cmf_get_current_user();
        if($this->request->isPost()){
            $cc=$this->request->Post();   
            session('phoneone', $cc['mobile']);				
            if(!empty($cc['mobile'])){
                $member1=DB::name('member')->where('mobile',$cc['mobile'])->find();             
                if(empty($member1)){
                    echo "<script>alert('没有此会员')</script>";
                }else{
                    $member=DB::name('member')->where('mobile',$cc['mobile'])->find();
                    $balance=DB::name('Finance')->where(array('userid'=>$member['id'],'storeid'=>$uinfo['store_id']))->find();
                    $member['balance']=$balance['money'];
                    
                    
                    $this->assign('vo',$member);
                    return $this->fetch('member/findmem');
                }
            }else{
                echo "<script>alert('手机号不能为空')</script>";
            }
        }
        return $this->fetch('member/recharge');
    }
  
    function findmem(){

        return $this->fetch();
    }
   public function chongzhi(){
       $uinfo=cmf_get_current_user();
       $res  = $this->request->param();
       $this->assign("mobile", $res['mobile']);
       $this->assign("store_id", $uinfo['store_id']);
      // dump($res['mobile']);
       return $this->fetch();
   } 
   function chongPost(){
        $uinfo=cmf_get_current_user();//管理员信息
        $config=DB::name('Config')->where(array('type'=>2,'state'=>1))->order('time desc')->find();//充值配置
        $data = $this->request->param();
        $unifo=DB::name('Member')->where(array('mobile'=>$data['mobile']))->find();//会员信息
        //dump($data);exit;
        //充值金额判断
        if($data['money']<$config['min']){//最小金额
            $this->error('充值金额不得少于'.$config['min']);
        }
        if($data['money']>$config['max']){//最小金额
            $this->error('充值金额不得大于'.$config['max']);
        } 
        $money=$data['money']*($config['top_money']/100); //赠的送钱
        //充值流水
        $cKM=DB::name('Finance')->where(array('userid'=>$unifo['id'],'storeid'=>$uinfo['store_id']))->find();
        if($cKM){
            $TMoney=$cKM['money']+$money+$data['money'];
            $re=DB::name('Finance')->where(array('userid'=>$unifo['id'],'storeid'=>$uinfo['store_id']))->setField('money',$TMoney);
        }else{
            //新增
            $da_M=array(
                'userid'=>$unifo['id'],
                'storeid'=>$uinfo['store_id'],
                'money'=>$money+$data['money']
            );
            Db::name('Finance')->insert($da_M);
        }
        //记录充值流水
        $da_M_rec_c=array(
            'userid'=>$unifo['id'],
            'storeid'=>$uinfo['store_id'],
            'money'=>$data['money'],
            'operatorid'=>$uinfo['id'],
            'remark'=>'充值',
            'ctime'=>time()
        );
        $re=Db::name('FinanceRecord')->insert($da_M_rec_c);
        
        if($money){
            //记录赠送余额流水
            $da_M_rec=array(
                'userid'=>$unifo['id'],
                'storeid'=>$uinfo['store_id'],
                'money'=>$money,
                'operatorid'=>$uinfo['id'],
                'remark'=>'充值赠送',
                'ctime'=>time()
            );
            Db::name('FinanceRecord')->insert($da_M_rec);           
        }
        
        //赠送积分
        $integral=$data['money']*($config['top_intintegral']/100);//赠送积分数量
        if($integral){
            //余额增加
            $cKin=DB::name('Integral')->where(array('userid'=>$unifo['id'],'storeid'=>$uinfo['store_id']))->find();
            if($cKin){
                $Tintegral=$cKin['num']+$integral;
                $re=DB::name('Integral')->where(array('userid'=>$unifo['id'],'storeid'=>$uinfo['store_id']))->setField('num',$Tintegral);
            }else{
                //新增
                $da_in=array(
                    'userid'=>$unifo['id'],
                    'storeid'=>$uinfo['store_id'],
                    'num'=>$integral
                );
                Db::name('Integral')->insert($da_in);
            }
            //记录流水
            $da_in_rec=array(
                'userid'=>$unifo['id'],
                'storeid'=>$uinfo['store_id'],
                'num'=>$integral,
                'operatorid'=>$uinfo['id'],
                'remark'=>'充值赠送',
                'ctime'=>time()
            );
            Db::name('IntegralRecord')->insert($da_in_rec);
        }
        $this->success('充值成功');   
   }
    function addition(){
        if($this->request->isPost()){
            $data=$this->request->Post();
            $param = $this->request->param();
            $param=$param['id'];           
            if(!empty($data['money'])){
                if(!empty($data['sendmoney'])){
                    if(!empty($data['allmoney'])){
                        if(!empty($data['allbalance'])){
                            if(!empty($data['ctime'])){
                                $uid=cmf_get_current_user_id();
                                $storeid=DB::name('user')->where('id',$uid)->column('storeid');
                               $mem['storeid']=$storeid[0];//店铺id
                                //var_dump($mem['storeid']);exit;
                                $mem['operatorid']=cmf_get_current_user_id();;//操作员id,登录用户id
                                //var_dump($mem['operatorid']);exit;
                                $mem['card_id']=$data['card_id'];
                                $mem['userid']=$param;
                                $mem['money']=$data['money'];
                                $mem['sendmoney']=$data['sendmoney'];
                                $mem['allmoney']=$data['allmoney'];
                                $mem['allbalance']=$data['allbalance'];
                                $mem['ctime']=$data['ctime'];
                                $mem['way']=$data['way'];
                                //var_dump($mem);exit;
                                $user=new RechargeModel;
                                $user->save($mem);
                                $user1=new MemberModel;
                                $user1->where('id',$param)->setField('balance',$data['allbalance']);
                                $this->success('充值成功',url('recharge'));
                                //echo "<script>alert('充值成功')</script>";


                            }else{
                                //echo "<script>alert('创建时间不能为空')</script>";
                                $this->error('创建时间不能为空',url('recharge'));
                            }

                        }else{
                            //echo "<script>alert('总余额不能为空')</script>";
                            $this->error('总余额不能为空',url('recharge'));
                        }

                    }else{
                        //echo "<script>alert('总金额不能为空')</script>";
                        $this->error('总金额不能为空',url('recharge'));
                    }

                }else{
                   // echo "<script>alert('赠送金额不能为空')</script>";
                    $this->error('赠送金额不能为空',url('recharge'));
                }
            }else{
                //echo "<script>alert('充值金额不能为空')</script>";
                $this->error('充值金额不能为空',url('recharge'));
            }

        }

    }


}
























